package com.foo.web.controller.api;

import java.sql.SQLException;
import java.util.List;

import com.foo.common.enums.OperatorType;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.foo.common.annotation.Log;
import com.foo.common.enums.BusinessType;
import com.foo.wechat.domain.WxPoultry;
import com.foo.wechat.service.IWxPoultryService;
import com.foo.common.core.controller.BaseController;
import com.foo.common.core.domain.AjaxResult;

/**
 * 家禽管理Api
 * 
 * @author foo
 * @date 2025-11-12
 */
@Controller
@RequestMapping("/wechat/api/WxPoultry")
public class WxPoultryApi extends BaseController
{
    @Autowired
    private IWxPoultryService wxPoultryService;

    /**
     * 查询家禽管理列表
     */
    @GetMapping("/list")
    @ResponseBody
    public AjaxResult list(WxPoultry wxPoultry,Integer pageNum, Integer pageSize)
    {
        if(pageNum != null){
            Integer size = 10;
            if(pageSize != null){
                size = pageSize;
            }
            PageHelper.startPage(pageNum, size);
        }
        List<WxPoultry> list = wxPoultryService.selectWxPoultryList(wxPoultry);
        return AjaxResult.success(list);
    }

    /**
     * 查询家禽管理详情
     */
    @GetMapping("/detail")
    @ResponseBody
    @Log(title = "家禽管理", businessType = BusinessType.Detail, operatorType = OperatorType.MOBILE)
    public AjaxResult detail(Long poultryId)
    {
        return AjaxResult.success(wxPoultryService.selectWxPoultryById(poultryId));
    }

    /**
     * 新增家禽管理
     */
    @PostMapping("/add")
    @ResponseBody
    @Log(title = "家禽管理", businessType = BusinessType.INSERT, operatorType = OperatorType.MOBILE)
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult addSave(WxPoultry wxPoultry) throws Exception
    {
        if(wxPoultryService.insertWxPoultry(wxPoultry) > 0){
            List<WxPoultry> wxPoultryList = wxPoultryService.selectWxPoultryList(wxPoultry);
            if(wxPoultryList.size() > 0){
                return AjaxResult.success(wxPoultryList.get(0));
            }else{
                //数据添加成功之后查询不到数据 回滚
                throw new SQLException("数据为空");
            }
        }
        return toAjax(0);
    }

    /**
     * 修改家禽管理
     */
    @PostMapping("/edit")
    @ResponseBody
    @Log(title = "家禽管理", businessType = BusinessType.UPDATE, operatorType = OperatorType.MOBILE)
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult editSave(WxPoultry wxPoultry)
    {
        return toAjax(wxPoultryService.updateWxPoultry(wxPoultry));
    }

    /**
     * 删除家禽管理(单个)
     */
    @PostMapping( "/remove")
    @ResponseBody
    @Log(title = "家禽管理", businessType = BusinessType.DELETE, operatorType = OperatorType.MOBILE)
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult remove(Long id)
    {
        return toAjax(wxPoultryService.deleteWxPoultryById(id));
    }
    
}
